package com.example.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class OpenDB extends SQLiteOpenHelper {

	// FOR THE TEAMS TABLE
	public static final String table1 = "teams";
	public static final String id = "_id";
	public static final String name = "name";
	public static final String won_matches = "won_matches";
	public static final String draw_matches = "draw_matches";
	public static final String lost_matches = "lost_matches";
	public static final String points = "points";
	public static final String rating = "rating";

	// FOR THE PLAYERS TABLE
	public static final String table2 = "players";
	public static final String position = "position";
	public static final String team = "team";

	private static final String DATABASE_NAME = "football.db";
	private static final int DATABASE_VERSION = 1;

	// STRING TO CREATE BOTH TABLES
	private static final String create_teams = "CREATE TABLE " + table1 + "("
			+ id + " INTEGER PRIMARY KEY AUTOINCREMENT, " + name
			+ " text not null, " + won_matches + " INTEGER, " + draw_matches
			+ " INTEGER, " + lost_matches + " INTEGER, " + points
			+ " INTEGER, " + rating + " INTEGER" + ");";

	private static final String create_players = "CREATE TABLE " + table2 + "("
			+ id + " INTEGER PRIMARY KEY AUTOINCREMENT, " + name
			+ " text not null, " + position + " text not null, " + team
			+ " text not null, " + rating + " INTEGER" + ");";

	public OpenDB(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(create_teams);
		db.execSQL(create_players);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w("OpenDB", "Update DB: " + oldVersion + " -> " + newVersion);
		db.execSQL("DROP TABLE IF EXISTS " + table1);
		db.execSQL("DROP TABLE IF EXISTS" + table2);

		onCreate(db);
	}

}
